package com.jtjrenren.android.taxi.driver.manger;

import android.content.Context;

import com.baidu.tts.answer.auth.AuthInfo;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.manger.task.CheckTTSFileTask;
import com.library.utils.LogUtil;
import com.jtjrenren.android.taxi.driver.utils.ModuleUtils;
import com.jtjrenren.android.taxi.driver.utils.ThreadManger;
import com.library.utils.FileUtils;

import java.io.File;

/**
 * Created by Administrator on 2016/8/26.
 */

public class TtsManger {
    private SpeechSynthesizer mSpeechSynthesizer;// 语音合成客户端
    private AuthInfo authInfo;
    private static TtsManger instance;
    private Context context;

    private TtsManger() {
    }

    public static TtsManger getInstance() {
        if (instance == null)
            instance = new TtsManger();
        return instance;
    }

    public void init(Context context) {
        this.context = context;
        ThreadManger.getInstance().exec(new CheckTTSFileTask(context));
    }

    public void start() {
        if (mSpeechSynthesizer != null)
            return;
        ThreadManger.getInstance().exec(new Runnable() {
            @Override
            public void run() {
                LogUtil.log(TtsManger.class, "tts客户端初始化");
                mSpeechSynthesizer = SpeechSynthesizer.getInstance();

                initTTS(context, mSpeechSynthesizer);

                authInfo = mSpeechSynthesizer.auth(TtsMode.MIX);   // 获取语音合成授权信息
                if (authInfo.isSuccess()) {
                    LogUtil.log(TtsManger.class, "授权成功");
                    mSpeechSynthesizer.initTts(TtsMode.MIX);
                } else {
                    LogUtil.log(TtsManger.class, "授权失败");
                    // 授权失败
                }
            }
        });
    }

    public void stop() {
        if (mSpeechSynthesizer != null)
            mSpeechSynthesizer.stop();
    }

    public void speak(String value) {
        if (mSpeechSynthesizer != null) {
            LogUtil.log(TtsManger.class, "speak:"+value);
            mSpeechSynthesizer.speak(value);
        }
        else
            start();
    }

    public void stopTts() {
        if (mSpeechSynthesizer != null) {
            mSpeechSynthesizer.release();
            mSpeechSynthesizer = null;
        }
    }

    //TTS语音
    public static final String TTS_PATH = FileUtils.getSDCardPath() + "/rrcx/tts";
    public static final String TTS_TEXT_MODEL_FILE = TTS_PATH + File.separator + "bd_etts_text.dat";
    public static final String TTS_SPEECH_MODEL_FILE = TTS_PATH + File.separator + "bd_etts_speech_female.dat";

    private void initTTS(Context context, SpeechSynthesizer mSpeechSynthesizer) {
        mSpeechSynthesizer.setApiKey(ModuleUtils.getStringByResource(context, R.string.tts_apikey),
                ModuleUtils.getStringByResource(context, R.string.tts_secretkey));
        mSpeechSynthesizer.setAppId(ModuleUtils.getStringByResource(context, R.string.tts_appid));

        mSpeechSynthesizer.setContext(context);
        mSpeechSynthesizer.setSpeechSynthesizerListener(new MyTtsListener());

        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE,
                TTS_TEXT_MODEL_FILE);// 设置语音合成文本模型文件
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE,
                TTS_SPEECH_MODEL_FILE);  // 设置语音合成声音模型文件
    }
    private class MyTtsListener implements SpeechSynthesizerListener {
        @Override
        public void onError(String arg0, SpeechError arg1) {
            LogUtil.log(TtsManger.class, "监听到出错，在此添加相关操作:"+arg0+"---Error:"+arg1.toString()+"-->"+arg1.code+","+arg1.description);
            // 监听到出错，在此添加相关操作
        }

        @Override
        public void onSpeechFinish(String arg0) {
            LogUtil.log(TtsManger.class, "监听到播放结束，在此添加相关操作");
            // 监听到播放结束，在此添加相关操作
        }

        @Override
        public void onSpeechProgressChanged(String arg0, int arg1) {
            LogUtil.log(TtsManger.class, "监听到播放进度有变化，在此添加相关操作");
            // 监听到播放进度有变化，在此添加相关操作
        }

        @Override
        public void onSpeechStart(String arg0) {
            LogUtil.log(TtsManger.class, "监听到合成并播放开始，在此添加相关操作");
            // 监听到合成并播放开始，在此添加相关操作
        }

        @Override
        public void onSynthesizeDataArrived(String arg0, byte[] arg1, int arg2) {
            LogUtil.log(TtsManger.class, " 监听到有合成数据到达，在此添加相关操作");
            // 监听到有合成数据到达，在此添加相关操作
        }

        @Override
        public void onSynthesizeFinish(String arg0) {
            LogUtil.log(TtsManger.class, " 监听到合成结束，在此添加相关操作");
            // 监听到合成结束，在此添加相关操作
        }

        @Override
        public void onSynthesizeStart(String arg0) {
            LogUtil.log(TtsManger.class, " 监听到合成开始，在此添加相关操作");
            // 监听到合成开始，在此添加相关操作
        }
    }
}
